import openpyxl
import time
import os
import random
 
current_time = time.strftime('%Y-%m-%d')
# 文件路径
file_path = str(str(current_time)+".xlsx")
# 判断文件是否存在
if os.path.exists(file_path):
    print(f"{file_path} 文件存在")
    # workbook = openpyxl.load_workbook('AA.xlsx')  # 加载已存在的工作簿
    workbook = openpyxl.load_workbook(file_path)
else:
    print(f"{file_path} 文件不存在")
    workbook = openpyxl.Workbook()  # 创建一个新的工作簿
 
 
 
# 获取活动工作表
sheet = workbook.active
print(sheet['AA1'].value)
if sheet['AA1'].value == None:
    start_row = 1
else:
    start_row = sheet['AA1'].value+1
 
while True:
    # 获取当前时间并格式化
    current_time = time.strftime('%H:%M:%S')
    # %Y - %m - %d
    content = random.randint(1, 999)
    # 将时间和内容写入工作表
    sheet.cell(row=start_row, column=1, value=current_time)
    sheet.cell(row=start_row, column=2, value=content)
    sheet['AA1'].value = start_row
    workbook.save(file_path)    # 保存工作簿
    start_row += 1              # 更新起始行以便下次写入新行
    time.sleep(2)
column_values = [str(sheet[f'A{str(i)}'].value)+ str(sheet[f'B{str(i)}'].value) for i in range(start_row-5 , start_row)]
print(column_values)
 